<?php
namespace Minhsang\ModelBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\User\AdvancedUserInterface;
use Doctrine\Common\Collections\ArrayCollection;
use APY\DataGridBundle\Grid\Mapping as GRID;

/**
 * @ORM\Table(name="users")
 * @ORM\Entity(repositoryClass="\Minhsang\ModelBundle\Repository\UserRepository")
 *
 * @GRID\Source(columns="id, username, email, role.name, isActive")
 */
class User implements AdvancedUserInterface, \Serializable
{
	/**
	 * @ORM\Column(type="integer")
	 * @ORM\Id
	 * @ORM\GeneratedValue(strategy="AUTO")
	 *
	 * @GRID\Column(filterable=false)
	 */
	private $id;

	/**
	 * @ORM\Column(type="string", length=25, unique=true)
	 */
	private $username;

	/**
	 * @ORM\Column(type="string", length=32)
	 */
	private $salt;

	/**
	 * @ORM\Column(type="string", length=100)
	 */
	private $password;

	/**
	 * @ORM\Column(type="string", length=60, unique=true)
	 */
	private $email;

	/**
	 * @ORM\ManyToMany(targetEntity="RolesList", inversedBy="users")
	 *
	 * @GRID\Column(field="roleslist.role", type="text", filter="select", title="role")
	 */
	private $role;

	/**
	 * @ORM\OneToMany(targetEntity="NewsCatalog", mappedBy="user")
	 * @ORM\Column(name="created_news", nullable=true)
	 */
	private $createdNews;

	/**
	 * @ORM\Column(name="is_active", type="boolean")
	 */
	private $isActive;


	public function __construct()
	{
		$this->isActive    = false;
		$this->salt        = md5(uniqid(null, true));
		$this->role      = new \Doctrine\Common\Collections\ArrayCollection();
		$this->createdNews = new \Doctrine\Common\Collections\ArrayCollection();
	}

	public function __toString()
	{
		return $this->username;
	}

	public function isAccountNonExpired()
	{
		return true;
	}

	public function isAccountNonLocked()
	{
		return true;
	}

	public function isCredentialsNonExpired()
	{
		return true;
	}

	public function isEnabled()
	{
		return $this->isActive;
	}

	/**
	 * @inheritDoc
	 */
	public function getRoles()
	{
		return $this->role->toArray();
	}

	/**
	 * @inheritDoc
	 */
	public function getUsername()
	{
		return $this->username;
	}

	/**
	 * @inheritDoc
	 */
	public function getSalt()
	{
		return $this->salt;
	}

	/**
	 * @inheritDoc
	 */
	public function getPassword()
	{
		return $this->password;
	}

	/**
	 * @inheritDoc
	 */
	public function getEmail()
	{
		return $this->email;
	}

	/**
	 * @inheritDoc
	 */
	public function eraseCredentials()
	{
	}

	/**
	 * @see \Serializable::serialize()
	 */
	public function serialize()
	{
		return serialize(array(
				$this->id,
		));
	}

	/**
	 * @see \Serializable::unserialize()
	 */
	public function unserialize($serialized)
	{
		list (
				$this->id,
		) = unserialize($serialized);
	}

	/**
	 * Get id
	 *
	 * @return integer
	 */
	public function getId()
	{
		return $this->id;
	}

	/**
	 * Set username
	 *
	 * @param string $username
	 * @return User
	 */
	public function setUsername($username)
	{
		$this->username = $username;

		return $this;
	}

	/**
	 * Set salt
	 *
	 * @param string $salt
	 * @return User
	 */
	public function setSalt($salt)
	{
		$this->salt = $salt;
		return $this;
	}

	/**
	 * Set password
	 *
	 * @param string $password
	 * @return User
	 */
	public function setPassword($password)
	{
		$this->password = $password;

		return $this;
	}

	/**
	 * Set email
	 *
	 * @param string $email
	 * @return User
	 */
	public function setEmail($email)
	{
		$this->email = $email;

		return $this;
	}

	/**
	 * Set isActive
	 *
	 * @param boolean $isActive
	 * @return User
	 */
	public function setIsActive($isActive)
	{
		$this->isActive = $isActive;

		return $this;
	}

	/**
	 * Get isActive
	 *
	 * @return boolean
	 */
	public function getIsActive()
	{
		return $this->isActive;
	}

	/**
	 * Add role
	 *
	 * @param \Minhsang\ModelBundle\Entity\RolesList $role
	 * @return User
	 */
	public function addRole(\Minhsang\ModelBundle\Entity\RolesList $role)
	{
		$this->role[] = $role;

		return $this;
	}

	/**
	 * Remove role
	 *
	 * @param \Minhsang\ModelBundle\Entity\RolesList $role
	 */
	public function removeRole(\Minhsang\ModelBundle\Entity\RolesList $role)
	{
		$this->role->removeElement($role);
	}

	/**
	 * Add createdNews
	 *
	 * @param \Minhsang\ModelBundle\Entity\NewsCatalog $createdNews
	 * @return User
	 */
	public function addCreatedNews(\Minhsang\ModelBundle\Entity\NewsCatalog $createdNews)
	{
		$this->createdNews[] = $createdNews;

		return $this;
	}

	/**
	 * Remove createdNews
	 *
	 * @param \Minhsang\ModelBundle\Entity\NewsCatalog $createdNews
	 */
	public function removeCreatedNews(\Minhsang\ModelBundle\Entity\NewsCatalog $createdNews)
	{
		$this->createdNews->removeElement($createdNews);
	}

	/**
	 * Get createdNews
	 *
	 * @return \Doctrine\Common\Collections\Collection
	 */
	public function getCreatedNews()
	{
		return $this->createdNews;
	}

	/**
	 * Get role
	 *
	 * @return \Doctrine\Common\Collections\Collection
	 */
	public function getRole()
	{
		return $this->role;
	}
}